
package cn.gz3c.uitest.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.huawei.hms.audioeditor.sdk.util.SmartLog;
import com.huawei.hms.audioeditor.ui.R.color;
import com.huawei.hms.audioeditor.ui.R.styleable;
import com.huawei.hms.audioeditor.ui.common.utils.DigitalLocal;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.gz3c.uitest.viewmodel.EditPreviewViewModel;

public class MLTimelineView extends View {
    private int a = 5;
    private Paint b = new Paint();
    private long c = 0L;
    private List<String> d = new ArrayList<>();
    private float e = 0.0F;
    private float f = 0.0F;
    private double g = 0.0D;
    private int h = 0;
    private int i = 0;
    private double j = 0.0D;
    private EditPreviewViewModel editPreviewViewModel;
    private boolean l = false;
    private final float m = (float) com.huawei.hms.audioeditor.ui.common.utils.a.a(70.0F);
    private StringBuilder n = new StringBuilder();
    private Activity o;
    private int p;

    public MLTimelineView(Context var1) {
        super(var1);
        this.a(var1, (AttributeSet) null);
    }

    public MLTimelineView(Context var1, @Nullable AttributeSet var2) {
        super(var1, var2);
        this.a(var1, var2);
    }

    public MLTimelineView(Context var1, @Nullable AttributeSet var2, int var3) {
        super(var1, var2, var3);
        this.a(var1, var2);
    }

    @SuppressLint({"SimpleDateFormat"})
    private final void a(Context var1, AttributeSet var2) {
        MLTimelineView var10000 = this;
        MLTimelineView var10001 = this;
        MLTimelineView var10002 = this;
        AttributeSet var10004 = var2;
        Activity var4;
        this.o = var4 = (Activity) var1;
        this.h = com.huawei.hms.audioeditor.ui.common.utils.a.a(var4);
        float var5;
        float var10009 = var5 = this.m;
        this.e = var5 * 0.5F;
        this.f = var10009 * 2.0F;
        this.g = (double) var5;
        this.setPadding(this.h / 2, 0, 20, 0);
        TypedArray var3 = var1.obtainStyledAttributes(var10004, styleable.MLTimelineView);
        var10002.b.setStrokeWidth(3.0F);
        var10001.b.setColor(var3.getColor(styleable.MLTimelineView_textColor, ContextCompat.getColor(var1, color.white)));
        var10000.b.setTextSize((float) com.huawei.hms.audioeditor.ui.common.utils.a.a(9.0F));
    }

    private void d() {
        this.editPreviewViewModel.m().observe((LifecycleOwner) this.o, (var1) -> {
            this.a(var1);
        });
    }

    private void f() {
        if (this.c <= 0L) {
            this.d.clear();
        } else {
            Calendar.getInstance();
            int var1;
            long var10002;
            long var2;
            label82:
            switch (this.a) {
                case 1:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 1000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.b((float) var10002, 5.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 1000L));
                        this.b(5);
                        ++var2;
                    }
                case 2:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 1000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.b((float) var10002, 4.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 1000L));
                        this.b(4);
                        ++var2;
                    }
                case 3:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 1000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.b((float) var10002, 2.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 1000L));
                        this.d.add("*");
                        ++var2;
                    }
                case 4:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 1000L + 1L);
                    this.j = (double) var10002;
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 1000L));
                        ++var2;
                    }
                case 5:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 2000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10002, 2.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 2000L));
                        ++var2;
                    }
                case 6:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 3000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10002, 3.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 3000L));
                        ++var2;
                    }
                case 7:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 5000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10002, 5.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 5000L));
                        ++var2;
                    }
                case 8:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 10000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10002, 10.0F);
                    this.d.clear();
                    var2 = 0L;

                    while (true) {
                        if (var2 >= (long) var1) {
                            break label82;
                        }

                        this.d.add(this.b(var2 * 10000L));
                        ++var2;
                    }
                case 9:
                    var10002 = this.c;
                    var1 = (int) (var10002 / 20000L + 1L);
                    this.j = (double) com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10002, 20.0F);
                    this.d.clear();

                    for (var2 = 0L; var2 < (long) var1; ++var2) {
                        this.d.add(this.b(var2 * 20000L));
                    }
            }

            this.i = this.d.size();
        }
    }

    private void b(int var1) {
        if (this.d != null) {
            for (int var2 = 1; var2 < var1; ++var2) {
                this.d.add("*");
            }
        }

    }

    private String b(long var1) {
        String var3 = DigitalLocal.format(0);
        if (var1 <= 0L) {
            return var3 + var3 + ":" + var3 + var3;
        } else {
            StringBuilder var10001 = this.n;
            var10001.delete(0, var10001.length());
            long var4;
            if ((var4 = var1 / 3600000L) > 0L) {
                if (var4 < 10L) {
                    this.n.append(var3);
                }

                this.n.append(DigitalLocal.format((float) var4));
                this.n.append(":");
                var1 %= 3600000L;
            }

            if ((var4 = var1 / 60000L) < 10L) {
                this.n.append(var3);
            }

            this.n.append(DigitalLocal.format((float) var4));
            this.n.append(":");
            if ((var1 = var1 % 60000L / 1000L) < 10L) {
                this.n.append(var3);
            }

            this.n.append(DigitalLocal.format((float) var1));
            return this.n.toString();
        }
    }

    private void a(@NonNull Canvas var1, int var2, float var3, float var4) {
        Double var5;
        Float var7;
        if ((var5 = com.huawei.hms.audioeditor.ui.common.utils.a.b(this.g, (double) var2)) == null) {
            var7 = null;
        } else {
            var7 = var5.floatValue();
        }

        float var8 = var7;

        for (int var6 = 0; var6 < var2 - 1; ++var6) {
            var1.drawLine(var3 += var8, var4 - 8.0F, var3, var4 + 8.0F, this.b);
        }

    }

    private void g() {
        this.post(() -> {
            this.f();
            LayoutParams var10004 = this.getLayoutParams();
            var10004.width = (int) this.b();
            this.setLayoutParams(var10004);
            this.requestLayout();
            this.invalidate();
            this.editPreviewViewModel.a(this.a);
        });
    }

    public double b() {
        return com.huawei.hms.audioeditor.ui.common.utils.a.d(this.j, com.huawei.hms.audioeditor.ui.common.utils.a.b(this.g, 1000.0D));
    }

    protected void onMeasure(int var1, int var2) {
        super.onMeasure(var1, var2);
        this.setMeasuredDimension((int) (this.b() + (double) this.h), this.getMeasuredHeight());
    }

    protected void onDraw(@Nullable Canvas var1) {
        super.onDraw(var1);
        float var2 = (float) this.getPaddingLeft();
        int var3 = com.huawei.hms.audioeditor.ui.common.utils.a.a(8.0F);

        for (int var4 = 0; var4 < this.i; ++var4) {
            String var5 = (String) this.d.get(var4);
            Rect var6;
            Rect var10000 = var6 = new Rect();
            this.b.getTextBounds(var5, 0, var5.length(), var6);
            float var11 = (float) var10000.height();
            Double var7;
            Float var12;
            if ((var7 = com.huawei.hms.audioeditor.ui.common.utils.a.b((double) var10000.width(), 2.0D)) == null) {
                var12 = null;
            } else {
                var12 = var7.floatValue();
            }

            float var13 = var12;
            if (var1 != null) {
                var2 -= var13;
                if ("*".equals(var5)) {
                    var1.drawLine(var2 + var13, (float) (var3 - 8), var2 + var13, (float) (var3 + 16), this.b);
                } else {
                    var1.drawLine(var2 + var13, (float) (var3 - 8), var2 + var13, (float) (var3 + 16), this.b);
                    this.b.setTypeface(Typeface.create("Huawei-Sans", Typeface.NORMAL));
                    var1.drawText(var5, var2, (float) var3 + var11 + 24.0F, this.b);
                }

                int var9;
                float var10;
                if ((var9 = this.a) == 1) {
                    var10 = var2 + var13;
                    var11 = (float) var3;
                    this.a(var1, 10, var10, var11);
                } else if (var9 == 2) {
                    var10 = var2 + var13;
                    var11 = (float) var3;
                    this.a(var1, 5, var10, var11);
                } else {
                    var10 = var2 + var13;
                    var11 = (float) var3;
                    this.a(var1, 2, var10, var11);
                }

                var2 = (float) ((double) var2 + this.g + (double) var13);
            }
        }

        if (this.p != this.a) {
            MutableLiveData var8;
            if ((var8 = this.editPreviewViewModel.i()) != null && var8.getValue() != null) {
                this.editPreviewViewModel.a((Long) var8.getValue());
            }

            this.p = this.a;
        }
    }

    public final void a(long var1) {
        if (var1 > 0L && this.c != var1) {
            this.c = var1;
            if (this.editPreviewViewModel.m() != null && this.editPreviewViewModel.m().getValue() != null) {
                this.a = (Integer) this.editPreviewViewModel.m().getValue();
            }

            this.g();
        }

    }

    public void a(int var1) {
        if (this.a != var1) {
            this.a = var1;
            this.f();
            this.g();
        }

    }

    public void a(EditPreviewViewModel editPreviewViewModel) {
        this.editPreviewViewModel = editPreviewViewModel;
        this.editPreviewViewModel.a(this.a);
        this.editPreviewViewModel.a(this.g);
        this.d();
    }

    @SuppressLint("ClickableViewAccessibility")
    public boolean onTouchEvent(MotionEvent var1) {
        return super.onTouchEvent(var1);
    }

    public final void a(double var1) {
        SmartLog.i("MLTimelineView", "updateOffset:  " + var1);
        boolean var3 = false;
        if (this.a != 1 || !com.huawei.hms.audioeditor.ui.common.utils.a.a(var1, 1.0D)) {
            if (this.a != 9 || !com.huawei.hms.audioeditor.ui.common.utils.a.a(1.0D, var1)) {
                if (this.l) {
                    this.g = com.huawei.hms.audioeditor.ui.common.utils.a.d(this.g, var1);
                    SmartLog.i("MLTimelineView", com.huawei.hms.audioeditor.ui.p.a.a("updateOffset width:  ").append(this.g).toString());
                    if ((var1 = this.g) >= (double) this.f) {
                        var3 = true;
                        --this.a;
                        this.g = (double) this.m;
                    } else if (var1 <= (double) this.e) {
                        var3 = true;
                        ++this.a;
                        this.g = (double) this.m;
                    }

                    if (var3) {
                        this.editPreviewViewModel.a(this.a);
                    }

                    this.editPreviewViewModel.a(this.g);
                }

            }
        }
    }

    public final void a(int var1, double var2) {
        this.a = var1;
        this.g = var2;
        this.f();
        LayoutParams var10001 = this.getLayoutParams();
        var10001.width = (int) this.b();
        this.setLayoutParams(var10001);
    }

    public final long a() {
        return this.c;
    }

    public double c() {
        return this.b();
    }

    public void a(boolean var1) {
        this.l = var1;
        SmartLog.i("MLTimelineView", "onScaleStatedChanged: " + var1);
    }
}
